/**
 * 
 */
package com.wubo.sec.filter;

import java.util.Iterator;
import java.util.Set;

import com.wubo.sec.SecurityManager;
import com.wubo.sec.core.RoleData;
import com.wubo.sec.model.Role;

/**
 * @author WuBo
 * @CreateDate 2010-8-8
 * @version 1.0.01
 */
public class RoleBeforeFilter implements IDataBeforeFilter {

	public String sift() {
		if( SecurityManager.isAdmin() ){
			return "";
		}else{
			Set<? extends RoleData> userRoles = SecurityManager.getCurrentUser().getRoles();
			if(userRoles != null){
				for(Iterator<? extends RoleData> ite=userRoles.iterator(); ite.hasNext();){
					RoleData roleData = ite.next();
					if( roleData instanceof Role ){
						if(Role.ADMIN_ROLE.equals(roleData.getCode())){
							return "";
						}
					}
				}
			}
			String username = SecurityManager.getCurrentUser().getUsername();
			if(SecurityManager.isMarkLocked()){ //标识码锁定
				return " and r.locked = 0 and r.owner = '"+username+"'";
			}else{//标识码未锁定
				return "and r.owner = '"+username+"'";
			}
		}
	}
}
